package com.zr.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zr.model.Course;
import com.zr.util.JDBCUtil;

import net.sf.json.JSONArray;
/**
 * 用户录入、修改成绩的dao层
 * @author Administrator
 *
 */
public class MarkDao {
	/**
	 * 创建录入、修改成绩的方法
	 * @param courseId 传入课程id 
	 * @param studentId 传入学生id
	 * @param score 传入录入的成绩
	 * @return 成功返回true
	 */
	public boolean updateScore(int courseId,int score) {
		boolean flag=false;
		String sql="UPDATE mark SET score = ? WHERE rid=?";
		Connection con=JDBCUtil.getJDBConnection();
		try {
			PreparedStatement pst=con.prepareStatement(sql);
			pst.setInt(1, score);
			pst.setInt(2, courseId);
			int i=pst.executeUpdate();
			if (i>0) {
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JDBCUtil.closedConnection(con);
		}
		return flag;
	}
	/**
	 * 查询当前老师下的课程
	 * @param teacherId  传入当前的老师 
	 * @return 返回课程json数组对象
	 */
	Map<String, String> course =  new HashMap<String, String>();
	public JSONArray getAllCourseByTid(int teacherId) {
		JSONArray jsa =new JSONArray();
		String sql="select course.cid,course.cname from course,teacher where course.tid=teacher.tid and course.tid=?";
		Connection con=JDBCUtil.getJDBConnection();
		try {
			PreparedStatement pst=con.prepareStatement(sql);
			pst.setInt(1, teacherId);
			ResultSet rs =pst.executeQuery();
			while(rs.next()){
				String cname = rs.getString("cname");
				String cid = String.valueOf(rs.getInt("cid"));
				course.put("id", cid);
				course.put("text", cname);
				jsa.add(course);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jsa;
	}
}
